package com.hjw.logtest1.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjw.logtest1.dao.UserDao;
import com.hjw.logtest1.entity.User;
import com.hjw.logtest1.error.BusinessException;
import com.hjw.logtest1.error.EmBusinessError;
import com.hjw.logtest1.model.UserModel;
import com.hjw.logtest1.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Hjw
 * @since 2023-04-07
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    private final UserDao userDao;

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public UserModel getUserModel(Integer userId) throws BusinessException{
        User byId = getById(userId);
        return convert2Model(byId);
    }

    private UserModel convert2Model(User user) throws BusinessException{
        if (user == null) {
            throw new BusinessException(EmBusinessError.USER_NOT_EXIST);
        }
        UserModel model = new UserModel();
        BeanUtils.copyProperties(user,model);
        return model;
    }
}
